package demo.java.parallelprogram.threadpool;

public class PThread extends  Thread{
	
	private ThreadPlool pool;//线程池
	private Runnable target;//任务
	private boolean isShutDown = false;
	private boolean isIdle = false;
	
	//构造函数
	public PThread(Runnable target,String name,ThreadPlool pool) {
		super(name);
		this.pool = pool;
		this.target = target;
	}
	
	@Override
	public void run() {
		super.run();
		//只要没关闭，则一直不结束改线程
		while (!isShutDown){
			isIdle = false;
			if(target != null){
				//运行任务
				target.run();
			}
			
			//任务接受，到闲置状态
			isIdle=true;
			try {
				pool.repool(this);
				synchronized (this) {
					wait();
				}
			} catch (InterruptedException e) {
			}
			
			isIdle = false;
		}
		
	}


	public synchronized void setShutDown() {
		this.isShutDown = true;
		notifyAll();
	}

	public synchronized void setTarget(Runnable newTarget) {
		this.target = newTarget;
		notifyAll();
	}

	public boolean isIdle() {
		return isIdle;
	}
	
	

}
